Trò chơi game bắn súng đơn giản trong UNITY Engine
23.587 lượt xem;
- EnemyBullet.cs
- EnemiesScr /
- Scripts /
- Assets /
- project /
1 using UnityEngine;
2 using System.Collections;
3
4 public class EnemyBullet : MonoBehaviour
5 {
6 Rigidbody rb;
7
8 void Awake ()
9 {
10 rb = GetComponent<Rigidbody>();
11 }
12
13 //ENEMY SHOOT BULLET BY CALLING THIS METHOD
14 public void ShootMe(Vector3 startPos, Vector3 dirrection, float speed = 7)
15 {
16 transform.position = startPos;
17 rb.velocity = dirrection * speed;
18 StartCoroutine("BulletLifeTime");
19 }
20
21 void OnTriggerExit(Collider col)
22 {
23 if (col.tag == "SpawnBorders")
24 {
25 BulletIsOff();
26 }
27 }
28
29 void OnTriggerEnter(Collider col)
30 {
31 if (col.tag == "Player")
32 {
33 col.GetComponent<IInterractWithBullet>().GetDamage();
34 }
35 }
36
37 void BulletIsOff()
38 {
39 rb.velocity = Vector3.zero;
40 gameObject.SetActive(false);
41 }
42
43 IEnumerator BulletLifeTime()
44 {
45 yield return new WaitForSeconds(10);
46 BulletIsOff();
47 }
48 }
2 using System.Collections;
3
4 public class EnemyBullet : MonoBehaviour
5 {
6 Rigidbody rb;
7
8 void Awake ()
9 {
10 rb = GetComponent<Rigidbody>();
11 }
12
13 //ENEMY SHOOT BULLET BY CALLING THIS METHOD
14 public void ShootMe(Vector3 startPos, Vector3 dirrection, float speed = 7)
15 {
16 transform.position = startPos;
17 rb.velocity = dirrection * speed;
18 StartCoroutine("BulletLifeTime");
19 }
20
21 void OnTriggerExit(Collider col)
22 {
23 if (col.tag == "SpawnBorders")
24 {
25 BulletIsOff();
26 }
27 }
28
29 void OnTriggerEnter(Collider col)
30 {
31 if (col.tag == "Player")
32 {
33 col.GetComponent<IInterractWithBullet>().GetDamage();
34 }
35 }
36
37 void BulletIsOff()
38 {
39 rb.velocity = Vector3.zero;
40 gameObject.SetActive(false);
41 }
42
43 IEnumerator BulletLifeTime()
44 {
45 yield return new WaitForSeconds(10);
46 BulletIsOff();
47 }
48 }
ENEMY SHOOT BULLET BY CALLING THIS METHOD